package com.springcloud.eshop.product.controller;

import com.springcloud.eshop.entity.Product;
import com.springcloud.eshop.product.service.ProductService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * (Product)表控制层
 *
 * @author makejava
 * @since 2020-06-28 23:35:18
 */
@RestController
@RequestMapping("/product")
public class ProductController {
    /**
     * 服务对象
     */
    @Resource
    private ProductService productService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @RequestMapping("/selectOne")
    @ResponseBody
    public Product selectOne(Long id) {
        return this.productService.queryById(id);
    }

    /**
     * @Desc: 插入数据
     * @Param brand
     * @Return com.springcloud.eshop.domain.entity.Brand
     * @Date: 2020/6/28
     */
    @RequestMapping("/insert")
    @ResponseBody
    public Product insert(Product product) {
        return this.productService.insert(product);
    }

    @RequestMapping("/update")
    @ResponseBody
    public Product update(Product product) {
        return this.productService.update(product);
    }

    @RequestMapping("/delete")
    @ResponseBody
    public boolean deleteById(Long id) {
        return this.productService.deleteById(id);
    }


    @RequestMapping("/selectProducts")
    @ResponseBody
    public List<Product> selectProducts(String productIds) {
        Set<String> productIdStringSet = new HashSet<>(Arrays.asList(productIds.split(",")));
        Set<Long> productIdSet = productIdStringSet.stream()
                .map(Long::valueOf)
                .collect(Collectors.toSet());
        return this.productService.selectProducts(productIdSet);
    }


}